package top.goldenyear.porpoise.dev.code.controller.admin;

import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import top.goldenyear.porpoise.common.core.model.Ret;
import top.goldenyear.porpoise.dev.code.dto.req.CodeGenCreateReq;
import top.goldenyear.porpoise.dev.code.dto.req.CodeGenUpdateReq;
import top.goldenyear.porpoise.dev.code.dto.req.DbTableSearchReq;
import top.goldenyear.porpoise.dev.code.model.CodeTable;
import top.goldenyear.porpoise.dev.code.service.CodeTableService;
import top.goldenyear.porpoise.framework.common.model.LoginUser;
import top.goldenyear.porpoise.framework.web.core.controller.BaseController;
import top.goldenyear.porpoise.framework.web.core.model.query.QPage;

/**
 * 表配置 Controller 控制层
 *
 * @author airhead
 * @since 2025-02-04
 */
@Component(tag = "adminApi")
@Mapping("/dev/code/codeTable")
@Slf4j
public class CodeTableAdminController extends BaseController {
  @Inject private CodeTableService service;

  @ApiOperation(value = "获取数据库表列表")
  @Get
  @Mapping("/dbTable/paginate")
  public Ret paginateDbTable(DbTableSearchReq searchReq, QPage qPage) {
    return Ret.okWithData(service.paginateDbTable(searchReq, qPage));
  }

  /**
   * 分页查询代码生成表定义
   *
   * @param searchReq 查询对象
   * @param qPage 分页对象
   * @return 分页对象
   */
  @Get
  @Mapping(value = "/paginate")
  @ApiOperation(value = "代码表列表 - 分页")
  public Ret paginate(CodeTable searchReq, QPage qPage) {
    return Ret.okWithData(service.paginateCodeTable(qPage, searchReq));
  }

  @Get
  @Mapping("/get")
  @ApiOperation(value = "获得代码表和字段定义")
  public Ret get(@Param("id") Integer id) {
    return Ret.okWithData(service.getCodeTable(id, LoginUser.of()));
  }

  @ApiOperation(value = "创建代码表和字段定义")
  @Post
  @Mapping("/create")
  public Ret create(@Body CodeGenCreateReq createReq) {
    return Ret.okWithData(service.createCodeTable(createReq, LoginUser.of()));
  }

  @ApiOperation(value = "更新代码表和字段定义")
  @Put
  @Post
  @Mapping("/update")
  public Ret update(@Body CodeGenUpdateReq updateReq) {
    service.updateCodeTable(updateReq, LoginUser.of());
    return Ret.okWithData(true);
  }

  @ApiOperation(value = "同步数据库的表和字段定义")
  @Post
  @Mapping("/sync")
  public Ret sync(@Param("id") Integer id) {
    service.syncCodeTable(id);
    return Ret.okWithData(true);
  }

  @ApiOperation(value = "删除代码表和字段定义")
  @Delete
  @Post
  @Mapping("/delete")
  public Ret delete(@Param("id") Integer id) {
    service.deleteCodeTable(id);
    return Ret.okWithData(true);
  }

  @Post
  @Delete
  @Mapping("batchDel")
  public Ret batchDel(@Param List<Long> ids) {
    return Ret.okWithData(service.batchDelCodeTable(ids));
  }

  @ApiOperation(value = "预览生成代码")
  @Get
  @Mapping("/preview")
  public Ret preview(Integer id) {
    return Ret.okWithData(service.previewCodeTable(id));
  }

  @ApiOperation(value = "客户端生成代码")
  @Get
  @Mapping("/download")
  public void download(Integer id, Context context) throws IOException {
    service.generateCode(id, context);
  }

  @ApiOperation(value = "服务端生成代码")
  @Get
  @Mapping("/generate")
  public Ret generate(Integer id) {
    return Ret.okWithData(service.generateFile(id));
  }

  @Get
  @Mapping("simpleList")
  @ApiOperation("简单列表")
  public Ret simpleList() {
    CodeTable codeTable = getBean(CodeTable.class);
    return Ret.okWithData(service.simpleListCodeTable(codeTable));
  }
}
